<html>
<head>
    <title>index</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"/>

    <!-- js -->
    <script type="text/javascript" src="../../js/lib/jquery.js"></script>
    <script type="text/javascript" src="../../js/config.js"></script>
    <script type="text/javascript" src="../../js/myUtils.js"></script>
</head>

<body>

<p>admin-login</p>

<p id="tips"></p>

<form id="login" action="/admin/login" method="post">
    <input type="text" name="loginId" placeHolder="name or email"><br>
    <input type="text" name="password" placeHolder="password"><br>
    <input type="button" id="doLogin" value="login">
</form>

<script type="text/javascript">
    $(function () {
        console.log("ready!");
        $('#doLogin').on('click', doLogin);
    });

    function doLogin() {
        console.log("doLogin");
        console.log(getHost());
        var formData = $("form#login").serializeObject();
        console.log(formData);

        $.ajax({
            url: getHost() + "/admin/login",
            type: "POST",
            headers: {
                "Content-Type": "application/json;charset=UTF-8"
            },
            data: JSON.stringify(formData),
            success: function (res) {
                console.log('succ', res);
                res = JSON.parse(res);
                if (res.errcode !== 0) {
                    $('#tips').text(res.errmsg);
                } else {
                    window.location = getHost() + '/admin'
                }
            },
            error: function (res) {
                console.log('error', res);
                $('#tips').text(res.errmsg);
            }
        });
    }
</script>

</body>
</html>